﻿using Newtonsoft.Json;

namespace Dyao.Book.Core
{
    public class DefaultAdditionDataFormatter : IAdditionDataFormatter
    {
        public static string DefaultNameFormat = "{0}_data";
        private readonly IAdditionDataValueFormatter _valueFormatter;
        private string _nameFormat;

        public string NameFormat => _nameFormat ?? DefaultNameFormat;

        public DefaultAdditionDataFormatter(IAdditionDataValueFormatter valueFormatter, string nameFormat = null)
        {
            if (!string.IsNullOrEmpty(nameFormat))
            {
                //todo:验证nameFormat格式

                this._nameFormat = nameFormat;
            }

            this._valueFormatter = valueFormatter;

        }

        public string FormatName(string path)
        {
            var adName = path;
            var index = path.LastIndexOf('.');
            if (index > 0 && index + 1 < path.Length)
            {
                adName = path.Substring(index + 1);
            }

            adName = string.Format(NameFormat, adName);

            return adName;

        }

        public object FormatValue(object value)
        {
            return _valueFormatter.Format(value);
        }

    }


}
